﻿package component.tree.web.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;


public class BaseTag extends BodyTagSupport {
	protected String id;
	protected String name;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public int doStartTag() throws JspException {
		return (EVAL_BODY_INCLUDE);
	}
	
	public int doEndTag() {
		return (EVAL_PAGE);
	}
	
	protected void print(String str){
		JspWriter out = pageContext.getOut();
		try {
			out.print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// 设置页面不缓存
	protected void setCache() {
		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = this.getResponse();
		if (request.getProtocol().compareTo("HTTP/1.0") == 0) {
			response.setHeader("Pragma", "no-cache");
		}
		if (request.getProtocol().compareTo("HTTP/1.1") == 0) {
			response.setHeader("Cache-Control", "no-cache");
		}
		response.setDateHeader("Expires", 0);
	}
	
	protected HttpServletRequest getRequest(){
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		return request;
	}
	
	protected HttpServletResponse getResponse(){
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		return response;
	}
	
	protected String getContext(){
		String context = this.getRequest().getContextPath();
		return context;
	}
	
	protected void setLineBreak(StringBuffer sb){
		sb.append("\n");
	}
	
	protected void startScript(StringBuffer sb){
		sb.append("<SCRIPT LANGUAGE='JavaScript'>");
		this.setLineBreak(sb);
		sb.append("<!--");
		this.setLineBreak(sb);
	}
	protected void endScript(StringBuffer sb){
		sb.append("//-->");
		this.setLineBreak(sb);
		sb.append("</SCRIPT>");
		this.setLineBreak(sb);
	}
}
